package Components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import smash_tris.*;

public class RoomsPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private ArrayList<Room> rooms;
	private ArrayList<JButton> buttons = new ArrayList<JButton>();
//	private String roomIcon;
	private Player user;
	private JPanel motherPanel;
	
	private ImageIcon image = new ImageIcon("picture/menu_pics/roomBar.png");
	private Integer buttonWidth = image.getIconWidth();
	private Integer buttonHeight = image.getIconHeight();
	
	private int width = 300;
	private int heigh = 80;

	public RoomsPanel(ArrayList<Room> rooms, JPanel mP) {
		
		this.setLayout(new GridBagLayout());
		this.motherPanel = mP;
	//	this.roomIcon = "picture/menu_pics/roomBar.png";
		this.rooms = rooms;
		this.setBackground(Color.WHITE); 
		
		this.addButton(new Room(new Player("a")), 0);
		this.addButton(new Room(new Player("a")), 1);
		this.addButton(new Room(new Player("a")), 2);
		System.out.println("Dodano");
		this.revalidate();
		//initComponents();
		// this.repaint();
	}
	
	
	private void addButton(Room room, Integer i){
		GridBagConstraints buttonC = new GridBagConstraints();
		JButton button = new JButton(image);
		button.setSize(new Dimension(this.buttonWidth, this.buttonHeight));
		buttons.add(button);
		buttonC.gridx = 0;
		buttonC.gridy = i;
		this.add(button, buttonC);
		
		
	}
	
	private class ButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			JButton button = (JButton) e.getSource();
			button.setEnabled(false);
			
		}
	}
	

	/**
	 * Metoda rysujaca panel z pokojami
	 */
	/*@Override
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;

		Image imgd = null;
		try {
			imgd = ImageIO.read(new File(roomIcon));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		BufferedImage img = new BufferedImage(getWidth(), getHeight(),
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D g22 = img.createGraphics();
		g22.drawImage(imgd, null, null);

		int w = img.getWidth(null);
		int h = img.getHeight(null);
		BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		Graphics tempG = bi.getGraphics();
		tempG.drawImage(img, 0, 0, null);

		float[] scales = { 0.5f, 0.5f, 0.5f, 0.3f };
		float[] offsets = new float[4];
		RescaleOp rop = new RescaleOp(scales, offsets, null);

		g2.drawImage(bi, rop, 0, 0);

	}*/

	public void setRooms(ArrayList<Room> roomowie) {
		rooms = roomowie;
		this.setPreferredSize(new Dimension(width, heigh * rooms.size()));

		//initComponents();
		this.revalidate();
	}
/*
	private void initComponents() {
		buttons.clear();
		this.removeAll();
		if ((rooms != null) && (rooms.size() > 0)) {
			this.setPreferredSize(new Dimension(width, heigh * rooms.size()));
			
			for (int i = 0; i < rooms.size(); i++) {
				ImageButton baton = new ImageButton(roomIcon, roomIcon);
				JLabel name = new JLabel();
				name.setText(rooms.get(i).getName());

				JLabel user = new JLabel();
				user.setText(rooms.get(i).getPlayers().length + "/"
						+ rooms.get(i).getNumPlay());
				final int number = i;

				name.setBounds(40, 30 + (i * heigh), name.getWidth(),
						name.getHeight());
				name.setBounds(220, 30 + (i * heigh), name.getWidth(),
						name.getHeight());
				baton.setBounds(20, 10 + (i * heigh), 251, 62);

				baton.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						buttonAction(evt, number);
					}
				});
				this.buttons.add(baton);
				this.add(baton);
				this.add(name);
				this.add(user);
				// this.add(baton);
			}
		}
	}
*/
	public void buttonAction(ActionEvent e, int i) {
		// TODO Send request to server with connect to room number i
		motherPanel.setVisible(false);

		System.out.println("KLIK");
		System.exit(-1);
	}
	

	
	

}
